package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.FriendlyLinks;
import com.example.buildtable.service.FriendlyLinksService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "友情链接")
@RequestMapping(value = "/friendlyLinks/")
public class FriendlyLinksController {
    @Autowired
    private FriendlyLinksService friendlyLinksService;
    
    @Operation(summary = "查询所有友情链接数据")
    @GetMapping(value = "/getAllFriendlyLinks")
    public R getAllFriendlyLinks() {
        List<FriendlyLinks> friendlyLinksList = friendlyLinksService.list();
        return R.ok("查询所有友情链接数据成功").data("friendlyLinksList", friendlyLinksList);
    }
    
    @Operation(summary = "增加友情链接")
    @PostMapping(value = "/addFriendlyLinks")
    public R addFriendlyLinks(FriendlyLinks friendlyLinks) {
        boolean flag = friendlyLinksService.save(friendlyLinks);
        if (flag) {
            return R.ok("增加友情链接成功");
        } else {
            return R.ok("增加友情链接失败");
        }
    }

    @Operation(summary = "删除友情链接")
    @GetMapping(value = "/deleteFriendlyLinks/{friendlyLinksId}")
    public R deleteFriendlyLinks(@PathVariable("friendlyLinksId") Integer friendlyLinksId) {
        boolean flag = friendlyLinksService.removeById(friendlyLinksId);
        if (flag) {
            return R.ok("删除友情链接成功");
        } else {
            return R.ok("删除友情链接失败");
        }
    }

    @Operation(summary = "按照友情链接查看")
    @GetMapping(value = "/getFriendlyLinksById/{friendlyLinksId}")
    public R getFriendlyLinksById(@PathVariable("friendlyLinksId") Integer friendlyLinksId) {
        FriendlyLinks friendlyLinks = friendlyLinksService.getById(friendlyLinksId);
        if (friendlyLinks != null) {
            return R.ok("按照友情链接查看成功").data("friendlyLinks", friendlyLinks);
        } else {
            return R.error("按照友情链接查看失败");
        }
    }

    @Operation(summary = "修改友情链接")
    @PostMapping(value = "/updateFriendlyLinks")
    public R updateFriendlyLinks(FriendlyLinks friendlyLinks) {
        boolean flag = friendlyLinksService.updateById(friendlyLinks);
        if (flag) {
            return R.ok("修改友情链接成功");
        } else {
            return R.ok("修改友情链接失败");
        }
    }
}